package com.oocl.tm.service.impl;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import com.oocl.tm.log4j.Log4j;
import com.oocl.tm.protocol.MsgPackage;
import com.oocl.tm.protocol.Operation;
import com.oocl.tm.repository.ClientsDBFactory;
import com.oocl.tm.service.NoticeService;
import com.oocl.tm.util.JsonUtil;

public class NoticeServiceImpl implements NoticeService{
	
	OutputStream out;
	
	
	public NoticeServiceImpl(OutputStream out){
		this.out = out;
	}


	@Override
	public void noticeOnline() throws IOException {
		HashMap<String, Boolean> clientList = new HashMap<String, Boolean>();
		for(String name:ClientsDBFactory.getClientsList().keySet()){
			clientList.put(name, ClientsDBFactory.getClientsList().get(name));
		}
	
		MsgPackage msgPackage =new MsgPackage(Operation.NOTIFY, null, null, null, null,clientList);
		Log4j.getLogger().info("[ Online message to all: ] "+msgPackage.toString());
		for (String name : clientList.keySet()) {
			if(ClientsDBFactory.getClientsList().get(name)){
//				System.out.println(name);
//				System.out.println("---"+ClientsDBFactory.getOutputsList().get(name));
				System.out.println(out);
				System.out.println(JsonUtil.parserToString(msgPackage));
				out.write(JsonUtil.parserToString(msgPackage).getBytes());
			}
		}
	}

}
